package easy;

public class test6 {
    public static String longestPalindrome(String s) {
        if(s.length()==1) return s;
        char[] arr=s.toCharArray();
        int len=0;// 回文串长度
        String ret="";
        for(int i=0;i<arr.length;i++) {
            int left=i;
            int right=i;
            if(i!=0) {
                int k=i;
                while(k>=1 && arr[k-1]==arr[i])  {
                    left=k-1;
                    k--;
                }
            }
            if(i!=arr.length-1) {
                int k=i;
                while(k+1<=arr.length-1 && arr[k+1]==arr[i]) {
                    right=k+1;
                    k++;
                }
            }
            int r=Math.min((left-0),(arr.length-1-right));// 确定最大边界
            while(r>=0) {
                if(arr[left]!=arr[right]) {
                    break;
                }
                if((right-left+1)>len) {
                    len=right-left+1;
                    ret=s.substring(left,right+1);
                }
                left--;
                right++;
                r--;
            }
        }
        if(ret=="") {
            return s.substring(0,1);
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(longestPalindrome("aaaa"));
    }
}
